package org.fioma.tree;

import java.io.BufferedWriter;
import java.io.IOException;

public class LeafBytes extends Leaf<byte[]> {

	public LeafBytes(InternalNode<?> parent, byte[] label) {
		this.parent = parent;
		this.label = label;
	}

	// add a new node in the position of the current label with label word.
	// The letter in the position of the current label will belong to the current label Node
	@Override
	public Leaf<?> addNode(int pos, byte[] word) {
		return (Leaf<?>) Tools.addNodeBytes(this, pos, word);
	}

	@Override
	public short getLengthLabel() {
		return label[0];
	}

	@Override
	public void addWord(byte[] word) {
		label = Tools.joinWords(label, word);
	}

	@Override
	public void printWords(BufferedWriter bw, byte[] prefix) throws IOException {
		byte[] b = Tools.joinWords(prefix, label);
		bw.write(Tools.transformToChars(b));
		bw.newLine();
	}

	@Override
	public boolean find(byte[] word) {
		return word == null || word[0] == 0;
	}

	@Override
	public int addSuffix(byte[] suffix) {
		label = Tools.joinWords(label, suffix);
		return 1;
	}

}
